using System.Collections.Generic;

namespace ViScript.Core
{
    public interface ISentence
    {
        string FileName { get; }

        int LineNumber { get; }

        string TrimmedContent { get; }

        bool ContainsSeparator(string text);
    }

    public class Sentence : ISentence
    {
        private readonly string fileName;
        private readonly int lineNumber;
        private readonly string content;
        private readonly string trimmedContent;

        public static readonly char[] SEPARATORS = new char[]
            {
                '.', ',', ':', ';', '!', '?'
            };

        private static readonly char[] TRIM_CHARS;

        static Sentence()
        {
            List<char> trimChars = new List<char>(SEPARATORS);
            trimChars.Add(' ');
            trimChars.Add('\t');
            TRIM_CHARS = trimChars.ToArray();
        }

        public Sentence(string fileName, int lineNumber, string content)
        {
            this.fileName = fileName;
            this.lineNumber = lineNumber;
            this.content = content;
            trimmedContent = Trim(content);
        }

        private static string Trim(string content)
        {
            return content.Trim(TRIM_CHARS);
        }

        public string FileName
        {
            get { return fileName; }
        }

        public int LineNumber
        {
            get { return lineNumber; }
        }

        public string TrimmedContent
        {
            get { return trimmedContent; }
        }

        public bool ContainsSeparator(string text)
        {
            foreach (char separator in SEPARATORS)
            {
                if (text.IndexOf(separator) != -1)
                {
                    return true;
                }
            }
            return false;
        }

        public string Content
        {
            get { return content; }
        }

        public override string ToString()
        {
            return string.Format("{0} <{1}, {2}>", content, fileName, lineNumber);
        }
    }
}